package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.PostBean;
import com.project.bean.ReplyBean;
import com.project.mapper.IPostMapper;
import com.project.mapper.IReplyMapper;
import com.project.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class PostService implements IPostService {
    @Autowired
    private IPostMapper postMapper;
    @Autowired
    private IReplyMapper replyMapper;

    @Override
    public IPage<PostBean> cutByItem(Integer pageNO, Integer channelId, String theme, String userName, LocalDate startDate, LocalDate endDate) {
        return postMapper.cutByItem(new Page<>(pageNO,2),channelId,theme,userName,startDate,endDate);
    }

    @Override
    public void add(PostBean postBean) {
            postMapper.insert(postBean);
    }

    @Override
    public PostBean findById(Integer id) {
        QueryWrapper<ReplyBean> qw = new QueryWrapper<>();
        qw.eq("fk_postId",id);
        List<ReplyBean> replyBeans = replyMapper.selectList(qw);
        PostBean byId = postMapper.findById(id);
        byId.setReplyList(replyBeans);
        return byId;
    }
}
